<?php

/**
* @copyright   (C) 2010 iJoomla, Inc. - All rights reserved.
* @license  GNU General Public License, version 2 (http://www.gnu.org/licenses/gpl-2.0.html) 
* @author  iJoomla.com webmaster@ijoomla.com
* @url   http://www.ijoomla.com/licensing/
* the PHP code portions are distributed under the GPL license. If not otherwise stated, all images, manuals, cascading style sheets, and included JavaScript  
* are NOT GPL, and are released under the IJOOMLA Proprietary Use License v1.0 
* More info at http://www.ijoomla.com/licensing/
*/

defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.modellist');
jimport('joomla.utilities.date');

class iJoomla_SurveysModelPrevsurvey extends JModel{
	
	function getSurveyDetails($survey_id){
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();		
		$query->select('*');
		$query->from('#__ijoomla_surveys_surveys');
		$query->where("id=".$survey_id);
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadAssocList();		
		return $result;
	}
	
	function getParams(){
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();		
		$query->select('params');
		$query->from('#__extensions');
		$query->where("element='com_surveys'");
		$db->setQuery($query);		
		$db->query();
		$result_string = $db->loadAssocList();
		$result = json_decode($result_string["0"]["params"]);		
		return $result;
	}
	
	function getAllPages($survey_id){
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();		
		$query->select('id');
		$query->from('#__ijoomla_surveys_pages');
		$query->where("survey_id=".$survey_id." and published=1");
		$query->order("ordering asc");
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadAssocList();		
		return $result;
	}
	
	function getPageDetails($page_id){
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();		
		$query->select('*');
		$query->from('#__ijoomla_surveys_pages');
		$query->where("id=".$page_id);
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadAssocList();		
		return $result;
	}
	
	function getPrevNextPage($survey_id, $current_page_id, $page_order){
		$pages = array();
		$db =& JFactory::getDBO();						
		$query = $db->getQuery(true);		
		
		//check for next page
		$query->clear();		
		$query->select('id');
		$query->from('#__ijoomla_surveys_pages');
		$query->where("survey_id=".$survey_id." and id <> ".$current_page_id." and ordering >=".$page_order." and published=1");
		$query->order("ordering asc");
		$db->setQuery($query, 0, 1);
		$db->query();
		$result = $db->loadResult();
		
		if(isset($result) && $result != NULL){			
			$pages["next"] = $result;
		}
		else{
			$pages["next"] = "last";
		}
		
		//check for preview page
		$query->clear();		
		$query->select('id');
		$query->from('#__ijoomla_surveys_pages');
		$query->where("survey_id=".$survey_id." and id <> ".$current_page_id." and ordering <=".$page_order." and published=1");
		$query->order("ordering desc");
		$db->setQuery($query, 0, 1);		
		$db->query();
		$result = $db->loadResult();		
		if(isset($result) && $result != NULL){			
			$pages["prev"] = $result;
		}
		else{
			$pages["prev"] = "first";
		}						
		return $pages;
	}
	
	
	function getAllSkipAction(){
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();		
		$query->select('*');
		$query->from('#__ijoomla_surveys_skip_logics');
		$db->setQuery($query);		
		$db->query();
		$result_array = $db->loadAssocList();
		$result = array();
		if(isset($result_array) && is_array($result_array) && count($result_array)>0){
			foreach($result_array as $key=>$value){
				$result[$value["question_id"]] = $value;
			}
		}
		return $result;
	} 
	
	function getDetails($survey_id){
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();		
		$query->select('title, show_result, form_target, redirection_url, redirection_msg, end_page_title, end_page_description');
		$query->from('#__ijoomla_surveys_surveys');
		$query->where("id=".$survey_id);
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadAssocList();		
		return $result;
	}	
	
	function getAllQuestions($survey_id){
		$data = new JDate();
		$db =& JFactory::getDBO();		
		$query = $db->getQuery(true);
		$query->clear();
		$query->select('*');
		$query->from('#__ijoomla_surveys_questions');
		$query->where("survey_id=".$survey_id." and published=1 and published_up <= '".$data."' and ((published_down >= '".$data."') or published_down='0000-00-00 00:00:00')");
		$query->order("ordering asc");
		$db->setQuery($query);		
		$db->query();
		$result = $db->loadAssocList();		
		return $result;
	}
	
	function getAllResponses($survey_id){
		$db =& JFactory::getDBO();
		$sql = "select count(*) from #__ijoomla_surveys_session where completed=1 and last_page_id=0 and published=1 and survey_id=".$survey_id;
		$db->setQuery($sql);
		$db->query();
		$result = $db->loadResult();
		return $result;
	}
	
	function getSurveyName($survey_id){
		$db =& JFactory::getDBO();
		$sql = "select title from #__ijoomla_surveys_surveys where id=".$survey_id;
		$db->setQuery($sql);
		$db->query();
		$result = $db->loadResult();
		return $result;
	}
	
}

?>